"use strict";
//对axios进行二次封装
import axios from "axios";
import { clearStore } from './locastorage';
// import Nprogress from "nprogress";
// import 'nprogress/nprogress.css'
import {ElMessage } from 'element-plus';
import { ElLoading  } from 'element-plus';
import { ref } from "vue";
import { useRouter, useRoute } from 'vue-router'
// 处理精度缺失的插件
import JSONBIG from 'json-bigint'
// import JSONBIG from 'json-bigint'
// baseURL: 'http://localhost:9010',
//baseURL: 'http://10.1.100.84:9999/metedisaster'
let service = axios.create({
  withCredentials: true,// 跨域请求时发送Cookie
  baseURL: '',
  timeout: 1000 * 60,
//   在传递给 then/catch 前，允许修改响应数据
  transformResponse: [
    data => {
    const jsonObject = JSONBIG.parse(data)
    return jsonObject;
    }
  ]
});
const router = useRouter()
let loading: { close: () => void; }
service.interceptors.request.use(
  function (config) {

    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
service.interceptors.response.use(
  function (response) {
    if (loading) {
        loading.close()
    }
    if (response.data.code == 0 && response.status == 200) {
      //如果有加载中组件，则关闭

    }
    else {
      // error
    }
    return response;
  },
  function (error) {
    if (loading) {
        loading.close()
    }
    if (error.response) {
      if (error.response.data.code == 10000 || error.response.data.code == 10001 || error.response.data.code == 10002) {
        clearStore();

      }
    }
    return error;
  }
);


export default {
  get (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      //全局loading
      loading = ElLoading.service({ fullscreen: true, background: 'rgba(0, 0, 0, 0.7)', text: '请稍后' })
      service.get(url, {
        data: data
      }, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res)
      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  // 任务监控特有的方法
  taskGet(url:string,data:object,header:{}){
    return new Promise((resolve) => {
      //全局loading
      service.get(url, {
        data: data
      }, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res)
      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  post (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      //全局loading
      loading = ElLoading.service({ fullscreen: true, background: 'rgba(0, 0, 0, 0.7)', text: '请稍后' })
      service.post(url, data, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res);

      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  postNL (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      service.post(url, data, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res);

      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  put (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      //全局loading
      loading = ElLoading.service({ fullscreen: true, background: 'rgba(0, 0, 0, 0.7)', text: '请稍后' })
      service.put(url, data, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res);

      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  delete (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      //全局loading
      loading = ElLoading.service({ fullscreen: true, background: 'rgba(0, 0, 0, 0.7)', text: '请稍后' })
      service.delete(url, data, header).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
          router.replace({ path: '/login' })
        }
        resolve(res);
      }, err => {
        if (err.response) {
            ElMessage({
            showClose: true,
            message: err.response.data.message,
            type: 'error'
          });
        }
      })
    })
  },
  Lpost (url:string, data:object, header:{}) {
    return new Promise((resolve) => {
      //全局loading
      loading = ElLoading.service({ fullscreen: true, background: 'rgba(0, 0, 0, 0.7)', text: '请稍后' })
      service.post(url, data, header)
        .then(res => {
          if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
          }
          resolve(res);

        }, err => {
          if (err.response) {
            ElMessage({
              showClose: true,
              message: err.response.data.message,
              type: 'error'
            });
          }
        })
    })
  },
  download (url:string, data:object, header:{}) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'get',
        url: url,
        data: data,
        responseType: 'blob', // 注意返回的数据格式
        headers: header
      }).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })

  },
  postDownload (url:string, data:object, header:{}) {
    return new Promise((resolve, reject) => {
      axios({
        method: 'post',
        url: url,
        data: data,
        responseType: 'blob', // 注意返回的数据格式
        headers: header
      }).then(res => {
        if (res.data.code == 9000) {
            ElMessage.error('登录失效，请重新登录');
            router.replace({ path: '/login' })
        }
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
}
